<?php
/**
 * Created by PhpStorm.
 * User: lili
 * Date: 2018/12/30
 * Time: 20:39
 */

namespace system\core\controller;


use system\core\route\Request;
use system\core\route\Route;
use system\core\view\View;

class Controller
{

    public $request;

    public $route;

    private $view ;

    public function __construct()
    {
        
        $this->request =  Request::getInstance();
        
        $this->route = Route::getInstance();

        $this->view = View::getInstance();

        $this->init();

    }
    
    /*
     * 初始化的前置函数
     * 专门被继承
     * */
    public function init(){

    }
    
    /*
     * 返回json数据
     * @param int $code 状态码
     * @param string $msg 提示信息
     * @param array $result 结果数据数组
     * @return json
     *
     * */
    public function json($code=0,$msg='',$result=array()){
        $a = array(
            'code' => $code,
            'msg' => $msg,
            'result' => $result
        );

        echo json_encode($a);
        exit();
    }

    
    public function assign($name,$value){
        $this->view->assign($name, $value);
    }


    /*
     * 重定向
     * @param string $url 跳转路径
     * */
    public function redirect($url){
        header('Location:'.$url);
    }

    /*
     * 
     * 返回
     * @param string $msg [提示信息]
     * 
     * 
     * */
    public function back($msg){
        echo "<script>alert('".$msg."');location.href='".$_SERVER["HTTP_REFERER"]."';</script>";
    }


    /*
     * 展示渲染模板
     *
     *
     * */
    public function display(){

        $routearr = $this->route->getRoutearr();
        /*$moudule = $this->route->getMoudule();
        $controller = $this->route->getController();
        $action = $this->route->getAction();*/

        $path = implode('/',$routearr );
        $this->view->display($path);

    }

}